/*
* Project: VD Kit
* Module: module_name
* File: wav.c
* Created Date: 2023-11-29 19:43:16
* Author: Huangjc
* Description: description
*
*历史记录：
* -----
* Modified: 2023-11-29 19:43:16
* Modified By: Huangjc
* Comments: v1.0 初版
* -----
*
* Copyright (c) 2023 - vDiscovery, Inc
*/


/* ======================================================================================
* includes
* 添加头文件位置
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


/* ======================================================================================
* macros
* 宏定义位置
*/


/* ======================================================================================
* log
* log标志位置
*/
#ifdef LOG_TAG
#undef LOG_TAG
#define LOG_TAG "TAG"
#endif


/* ======================================================================================
* extern
* 外部定义变量或函数位置
*/


/* ======================================================================================
* declaration
* 函数声明位置
*/


/* ======================================================================================
* types
* 类型定义位置
*/

typedef struct 
{
    char chunkID[4];
    int chunkSize;
    char format[4];
}WAVriff_t;

typedef struct 
{
    char subchunk1ID[4];
    int subchunk1Size;
}WAVsubchunk1header_t;

typedef struct
{ 
    short audioFormat;
    short numChannels;
    int sampleRate;
    int byteRate;
    short blockAlign;
    short bitsPerSample;
}WAVsubchunk1_t;

typedef struct 
{
    char subchunk2ID[4];
    int subchunk2Size;
}WAVsubchunk2header_t;


typedef struct
{
    char *file_name;
    WAVriff_t *riff;
    WAVsubchunk1_t *subcunk1;
}WAV_t;

/* ======================================================================================
* globals
* 全局变量位置
*/

static WAV_t Wav_file;
static WAVriff_t wav_riff;
static WAVsubchunk1_t wav_subchunk1;

/* ======================================================================================
* private implementation
* 本模块私有函数位置
*/
static int wav_parse_riff(FILE *fp,WAVriff_t *riff)
{
    int len = 0;

    if( fp == NULL)
    {
        printf("file not open \n");
        return -1;
    }

    len = fread(riff, sizeof(WAVriff_t), 1, fp);

    if( len > 0 )
    {
        printf("============== format ==============\n");
        printf("RIFF \"%c%c%c%c\" \n",riff->chunkID[0],riff->chunkID[1],riff->chunkID[2],riff->chunkID[3]);
        printf("All size %d \n",riff->chunkSize);
        printf("format \"%c%c%c%c\" \n",riff->format[0],riff->format[1],riff->format[2],riff->format[3]);
        printf("============== end ==============\n");
    }
    else
    {
        return -1;
    }

    printf("\n");

    if( strncmp(riff->chunkID,"RIFF",4) )
    {
        printf("Wav header wrong\n");
        return -1;
    }

    return 0;
}

static int wav_parse_fmt(FILE *fp,WAVsubchunk1_t *subchunk1)
{
    int len = 0;
    int chunk1_size = 0;
    char *buffer = NULL;

    buffer = (char *)malloc(Wav_file.riff->subchunk1Size);
    if(buffer == NULL)
    {
        printf("buffer malloc failed\n");
    }

    len = fread(buffer, Wav_file.riff->subchunk1Size, 1, fp);
    if( len > 0 )
    {
        memcpy(subchunk1,buffer,sizeof(WAVsubchunk1_t));
        free(buffer);
        buffer = NULL;

        printf("============== fmt ==============\n");
        printf("audioFormat %d \n",subchunk1->audioFormat);
        printf("numChannels %d \n",subchunk1->numChannels);
        printf("sampleRate %d \n",subchunk1->sampleRate);
        printf("byteRate %d \n",subchunk1->byteRate);
        printf("blockAlign %d \n",subchunk1->blockAlign);
        printf("bitsPerSample %d \n",subchunk1->bitsPerSample);
        printf("============== end ==============\n");

    }
    else
    {
        free(buffer);
        buffer = NULL;
        return -1;
    }

    printf("\n");

    if( subchunk1->audioFormat != 1 )
    {
        printf("wav audio format not PCM \n");
        return -1;
    }

    return 0;

}

int main(int argc,char **argv)
{   
    int ret = 0;

    if(argc != 2)
    {
        printf("input file \n");
        return -1;
    }

    printf("%s \n",argv[argc-1]);

    memset(&Wav_file,0,sizeof(WAV_t));

    FILE* file = fopen(argv[argc-1], "rb");
    if (file == NULL) {
        printf("Failed to open WAV file.\n");
        return -1;
    }

    Wav_file.file_name = argv[argc-1];

    //解析riff
    ret = wav_parse_riff(file,&wav_riff);
    if(ret)
    {
        printf("riff parse failed\n");
        goto end;
    }
    Wav_file.riff = &wav_riff;

    //解析subchunk1
    if( strncmp(wav_riff.subchunk1ID,"fmt",3) == 0 )
    {
        ret = wav_parse_fmt(file,&wav_subchunk1);
    }
    else if( strncmp(wav_riff.subchunk1ID,"data",4) == 0 )
    {
        //ret = wav_parse_data();
    }
    
    if(ret)
    {
        printf("subchunk1 parse failed\n");
        goto end;
    }

    Wav_file.subcunk1 = &wav_subchunk1;

end:
    fclose(file);

    return 0;
}

/* ======================================================================================
* implementation
* 本模块对外函数位置
*/

